package com.open.free.generator.controller;

import com.open.free.generator.base.BaseController;
import com.open.free.generator.entity.Columns;
import com.open.free.generator.entity.Source;
import com.open.free.generator.entity.Tables;
import com.open.free.generator.result.ResultData;
import com.open.free.generator.service.ISourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Faye.Wang
 * @version 1.0
 * @date 2020/12/22 22:39
 * @Description
 */
@Controller
public class SqlController extends BaseController {
    @Autowired
    private ISourceService iSourceService;


    @RequestMapping("/getSqlResult")
    @ResponseBody
    public ResultData getSqlResult(@RequestParam Integer page,@RequestParam Integer limit){
        return super.resultSuccess();
    }
    @RequestMapping("/insertDataSource")
    @ResponseBody
    public ResultData insertDataSource(@RequestBody Source source){
        iSourceService.insertSource(source);
        return super.resultSuccess();
    }

    @RequestMapping("/getDataSource")
    @ResponseBody
    public ResultData getDataSource(){
        List<Source> sourceList =  iSourceService.getDataSourceList();
        return super.resultSuccess(sourceList);
    }

    @RequestMapping("/delDataSource")
    @ResponseBody
    public ResultData delDataSource(@RequestParam Integer id){
        iSourceService.delDataSource(id);
        return super.resultSuccess();
    }

    @RequestMapping("/conDataSource")
    @ResponseBody
    public ResultData conDataSource(@RequestParam Integer id) throws SQLException {
        Source source = iSourceService.getSourceById(id);
        List<Tables> tablesList = iSourceService.getTables(source);
        Map<String,Object> tablesMap = new HashMap<>();

        tablesList.forEach(item->{
            List<Columns> columnsList = item.getColumns();

            List<String> tempColumns = columnsList.stream().map(Columns::getSqlColumn).collect(Collectors.toList());
            String[] columns = new String[tempColumns.size()];
            columns = tempColumns.toArray(columns);
            tablesMap.put(item.getTableName(),columns);
        });
        Map<String,Object> jsonData = new HashMap<>();
        jsonData.put("tablesList",tablesList);
        jsonData.put("tablesMap",tablesMap);
        return super.resultSuccess(jsonData);
    }

    /**
     * 执行SQL
     * @param sql
     * @param id
     * @return
     */
    @RequestMapping("/runSqlResult")
    @ResponseBody
    public ResultData runSqlColumns(@RequestParam String sql,@RequestParam Integer id) throws Exception {
        Source source = iSourceService.getSourceById(id);
        Map<String,Object> columns = iSourceService.getSqlResultColumns(source,sql);

        return super.resultSuccess(columns);
    }

    /**
     *
     * @param sql
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/getTablesBean")
    @ResponseBody
    public ResultData getTablesBean(@RequestParam String table,@RequestParam Integer id) throws Exception {
        Source source = iSourceService.getSourceById(id);

        List<Columns> columns = iSourceService.getTablesBean(source,table);

        return super.resultSuccess(columns);
    }

}
